package com.simoniu.first;

import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "helloServlet", urlPatterns = {"/hello", "/haha", "/hehe"})
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //保存在request
        req.setAttribute("city", "西安");
        //保存在session
        String sessionId = req.getSession().getId(); //获取当前会话ID。
        req.getSession().setAttribute("sessionId", sessionId);

        //保存在application
        String date = LocalDate.now().format(DateTimeFormatter.ISO_DATE);
        req.getServletContext().setAttribute("date", date);
        //通过请求重定向测试三个作用域。
        //resp.sendRedirect(req.getContextPath() + "/test");

        //请求转发
        req.getRequestDispatcher("/test").forward(req,resp);


    }
}